package com.example.bes013.web.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.*;

@RestController
public class MBeanDataController {

    @GetMapping(value = "/indicators")
    public void testGetMbean() throws MalformedObjectNameException, ReflectionException, AttributeNotFoundException, InstanceNotFoundException, MBeanException {
        MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        ObjectName executorObjectName = new ObjectName("BES:type=Executor,name=http-thread-pool");
        int maxThreads = (int) mBeanServer.getAttribute(executorObjectName, "maxThreads");
        System.out.println("Executor 的最大线程数: " + maxThreads);

        ObjectName threadPoolObjectName = new ObjectName("BES:type=ThreadPool,name=\"http-nio2-0.0.0.0-10012\"");
        int currentThreadCount = (int) mBeanServer.getAttribute(threadPoolObjectName, "currentThreadCount");
        System.out.println("ThreadPool 的当前线程数：" + currentThreadCount);

        ObjectName o1 = new ObjectName("BES:type=ThreadPool,name=\"http-nio2-0.0.0.0-10012\"");
        int currentBusyThreadCount = (int) mBeanServer.getAttribute(o1, "currentThreadsBusy");
        System.out.println("ThreadPool 的当前活动线程数：" + currentBusyThreadCount);
    }
}
